package com.qy127.javaweb.controller;


import com.qy127.javaweb.dao.impl.T_roleDaoimpl;
import com.qy127.javaweb.entity.T_role;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/t/*")
 public class T_roleServlet extends HttpServlet {
    private final T_roleDaoimpl tdaoimpl = new T_roleDaoimpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uri = req.getRequestURI();
        int i = uri.lastIndexOf("/");
        String s = uri.substring(i + 1);
        switch (s){
            case "cha":
                cha(req,resp);
               break;
            case "zeng":
                zeng(req,resp);
                break;
            case "delete":
                delete(req,resp);
                break;
            case "gouspdte":
                gouspdte(req,resp);
                break;
            case "gai":
                gai(req,resp);
                break;
            default:
                break;
        }

    }
    protected void gai(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String memo = req.getParameter("memo");
        String s = req.getParameter("status");
        System.out.println(id + name+ memo+s);
        int i = tdaoimpl.ua(name,memo,Integer.parseInt(s),Integer.parseInt(id));
        if (i>0){
            cha(req,resp);
        }
    }

    protected void gouspdte(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String id = req.getParameter("id");
        T_role getone = tdaoimpl.getone(Integer.parseInt(id));
        String memo = getone.getMemo();
        String rname = getone.getRname();
        Integer status = getone.getStatus();
        req.setAttribute("name",rname);
        req.setAttribute("memo",memo);
        req.setAttribute("status",status);
        req.setAttribute("id",Integer.parseInt(id));
        req.getRequestDispatcher("/roleupdet.jsp").forward(req,resp);

    }
    /**
     * 查询所有
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void cha(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<T_role> getall = tdaoimpl.getall();
        req.setAttribute("get",getall);
        req.getRequestDispatcher("/role.jsp").forward(req,resp);
    }

    /**
     * 添加一位人员
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void zeng(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String name = req.getParameter("name");
        String memo = req.getParameter("memo");
        String status = req.getParameter("status");
        T_role role = new T_role(null, name, memo, Integer.parseInt(status));
        int i = tdaoimpl.add(role);
        if (i>0){
            req.setAttribute("mm","添加成功");
        }else {
            req.setAttribute("mm","添加失败");
        }
        req.getRequestDispatcher("/addrole.jsp").forward(req,resp);
    }

    /**
     * 删除一名人员
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void delete (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String id = req.getParameter("id");
        int i = tdaoimpl.dele(Integer.parseInt(id));
        if (i>0){
            cha(req,resp);
        }
    }
 }
